#!/bin/bash


# Copyright (c) 2024 Huawei Device Co., Ltd. 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http:// www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
#
# limitations under the License.


set -e

export PATH=../../../../prebuilts/clang/ohos/linux-x86_64/llvm/bin/:$PATH
export PRODUCT_PATH=vendor/archermind/long2k1500
IMAGE_SIZE=64  # 64M
IMAGE_BLOCKS=4096
ENABLE_LTO_O0=${3}

CPUs=`sed -n "N;/processor/p" /proc/cpuinfo|wc -l`
MAKE="make LC_ALL=C LANG=C CROSS_COMPILE=../../../../device/soc/longson/toolchain/loongson-gnu-toolchain-cross-tools/bin/loongarch64-unknown-linux-gnu-"
[ "${ENABLE_LTO_O0}" == "enable_lto_O0" ] && MAKE="${MAKE} KCFLAGS=-Wl,--lto-O0"
BUILD_PATH=boot_linux
EXTLINUX_PATH=${BUILD_PATH}
EXTLINUX_CONF=${EXTLINUX_PATH}/boot.cfg
TOYBRICK_DTB=loongson.dtb
if [ ${KBUILD_OUTPUT} ]; then
	OBJ_PATH=${KBUILD_OUTPUT}/
fi

ID_MODEL=1
ID_ARCH=2
ID_UART=3
ID_DTB=4
ID_IMAGE=5
ID_CONF=6
model_list=(
	"long2k1500   loongarch 0xfe660000 loongson vmlinuz loongson2k1500_defconfig"
)


function help()
{
	echo "Usage: ./make-ohos.sh {BOARD_NAME}"
	echo "e.g."
	for i in "${model_list[@]}"; do
		echo "  ./make-ohos.sh $(echo $i | awk '{print $1}')"
	done
}

function make_kernel_image()
{
	arch=$1
	conf=$2
	dtb=$3
	
	config_base="arch/${arch}/configs/${conf}"
	config_frag=""
	if [ "$GPUDRIVER" == "mesa3d" ]; then 
		config_frag="../../../../device/soc/rockchip/panfrost.config"
	fi
	ARCH=${arch} ./scripts/kconfig/merge_config.sh ${config_base} ${config_frag}

	if [ $? -ne 0 ]; then
		echo "FAIL: ${MAKE} ARCH=${arch} ${conf}"
		return -1
	fi

	${MAKE} ARCH=${arch} vmlinuz dtbs -j${CPUs}
	if [ $? -ne 0 ]; then
		echo "FAIL: ${MAKE} ARCH=${arch} ${dtb}.img"
		return -2
	fi

	return 0
}

function make_ext2_image()
{
	blocks=${IMAGE_BLOCKS}
	block_size=$((${IMAGE_SIZE} * 1024 * 1024 / ${blocks}))

	if [ "`uname -m`" == "aarch64" ]; then
		echo y | sudo mke2fs -b ${block_size} -d boot_linux -i 8192 -t ext2 boot_linux.img ${blocks}
	else
		genext2fs -B ${blocks} -b ${block_size} -d boot_linux -i 8192 -U boot_linux.img
	fi

	return $?
}

function make_boot_linux()
{
	arch=${!ID_ARCH}
	uart=${!ID_UART}
	dtb=${!ID_DTB}
	image=${!ID_IMAGE}
	conf=${!ID_CONF}
	if [ ${arch} == "arm" ]; then
		dtb_path=arch/arm/boot/dts
	fi

	rm -rf ${BUILD_PATH}
	mkdir -p ${EXTLINUX_PATH}

	make_kernel_image ${arch} ${conf} ${dtb}
	if [ $? -ne 0 ]; then
		exit 1
	fi
	cp ../../../../device/board/archermind/long2k1500l1/tools/boot_from_emmc/boot.cfg ${EXTLINUX_CONF}
	cp -f ${OBJ_PATH}/${image} ${BUILD_PATH}/
	#cp -f ${OBJ_PATH}${dtb_path}/${dtb}.dtb ${EXTLINUX_PATH}/${TOYBRICK_DTB}
	#cp -f logo*.bmp ${BUILD_PATH}/
	if [ "enable_ramdisk" != "${ramdisk_flag}" ]; then
		make_ext2_image
	fi
}

ramdisk_flag=$2
found=0
for i in "${model_list[@]}"; do
	if [ "$(echo $i | awk '{print $1}')" == "$1" ]; then
		make_boot_linux $i
		found=1
	fi
done

